package com.source.multithread.download;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;


public class ExcuterDemo01 {
	public static void main(String[] args) {
		handleNum();
	}

	public static void handleNum(){
		CountDownLatch countDown = new CountDownLatch(5);
		Semaphore semaphore = new Semaphore(3);
		ExecutorService executor = Executors.newFixedThreadPool(10);
		//循环这个集合-查询主体数据项

		//执行查询操作
		executor.execute(new PrintNum(countDown, semaphore));

		try {
			countDown.await();
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		executor.shutdown();
	}






	static class  PrintNum  implements Runnable {
		public  int total=50;
		private CountDownLatch countDown;
		private Semaphore semaphore;

		public PrintNum(CountDownLatch countDown, Semaphore semaphore) {
			this.countDown=countDown;
			this.semaphore=semaphore;
		}

		public CountDownLatch getCountDown() {
			return countDown;
		}

		public void setCountDown(CountDownLatch countDown) {
			this.countDown = countDown;
		}

		public Semaphore getSemaphore() {
			return semaphore;
		}

		public void setSemaphore(Semaphore semaphore) {
			this.semaphore = semaphore;
		}

		@Override
		public void run() {
			try {
				semaphore.acquire();
				for(int i=0;i<total;i++){
					System.out.println(Thread.currentThread().getName()+"....."+i);
				}
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}finally{
				try {
					countDown.countDown();
					semaphore.release();
				} catch (Exception e2){}
			}


		}

	}

}
